<!DOCTYPE html>
<!--
  The MIT License (MIT)

  Copyright (c) 2017 Tarek Sherif

  Permission is hereby granted, free of charge, to any person obtaining a copy of
  this software and associated documentation files (the "Software"), to deal in
  the Software without restriction, including without limitation the rights to
  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  the Software, and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<!-- 
    Based on "Weighted Blended Order-Independent Transparency"
    By Morgan McGuire and Louis Bavoil 
    http://jcgt.org/published/0002/02/09/
-->
<html>
<head>
    <title>WebGL 2 Example: Order-independent Transparency</title>
    <meta charset="utf-8">
    <script src="utils/gl-matrix.js"></script>
    <script src="utils/utils.js"></script>
    <link rel="stylesheet" href="css/webgl2examples.css">
</head>
<body>
    <div id="example-title">
        <header>WebGL 2 Example: Order-independent Transparency</header>
        <div id="features">
            Features: Vertex Arrays, Uniform Buffers, Immutable Textures, Multiple Render Targets, Float Textures, texelFetch, Hardware Instancing, EXT_color_buffer_float
        </div>
        <div>
            <a href="https://github.com/tsherif/webgl2examples/blob/master/oit.html">Source code</a>
        </div>
    </div>
    <canvas id="gl-canvas"></canvas>
    <script type="x-shader/x-vertex" id="vertex-accum">
        #version 300 es

        layout(std140, column_major) uniform;
        
        layout(location=0) in vec4 position;
        layout(location=1) in vec2 uv;
        layout(location=2) in vec3 normal;
        layout(location=3) in vec4 color;
        layout(location=4) in mat4 modelMatrix;
        
        uniform SceneUniforms {
            mat4 uViewProj;
            vec4 uEyePosition;
            vec4 uLightPosition;
        };       
        // 这就等于是取代varying的语法
        out vec3 vPosition;
        out vec2 vUV;
        out vec3 vNormal;
        flat out vec4 vColor;

        void main() {
            vec4 worldPosition = modelMatrix * position;
            vPosition = worldPosition.xyz;
            vUV = uv;
            vNormal = mat3(modelMatrix) * normal;
            vColor = color;
            gl_Position = uViewProj * worldPosition;
        }
    </script>
    <script type="x-shader/x-fragment" id="fragment-accum">
        #version 300 es
        precision highp float;
         layout(location =2) out vec4 color2 ;
         #define gl_FragColor color2 
        layout(std140, column_major) uniform;

        uniform SceneUniforms {
            mat4 uViewProj;
            vec4 uEyePosition;
            vec4 uLightPosition;
        };

        uniform sampler2D uTexture;
        
        in vec3 vPosition; // in 取代了attribute
        in vec2 vUV;
        in vec3 vNormal;
        flat in vec4 vColor;
        // 下面会用到accumColor的的alpha  所以需要单独输出一个accumAlpha 所以非得drawbuffers不可
        layout(location=0) out vec4 accumColor;
        layout(location=1) out float accumAlpha;
        //输入 深度和 alpha    输出权重
        float weight(float z, float a) {
            return clamp(pow(min(1.0, a * 10.0) + 0.01, 3.0) * 1e8 * pow(1.0 - z * 0.9, 3.0), 1e-2, 3e3);
        }

        void main() {
            vec3 position = vPosition.xyz;
            vec3 normal = normalize(vNormal.xyz);
            vec2 uv = vUV;
            //  用非归一化 uv坐标 直接读取纹理像素 看下面的用法是直接传FragCoord的值了
            vec4 baseColor = vColor * texture(uTexture, uv);
            vec3 eyeDirection = normalize(uEyePosition.xyz - position);
            vec3 lightVec = uLightPosition.xyz - position;
            vec3 lightDirection = normalize(lightVec);
            vec3 reflectionDirection = reflect(-lightDirection, normal);
            float nDotL = max(dot(lightDirection, normal), 0.0);
            float diffuse = nDotL;
            float ambient = 0.2;
            float specular = pow(max(dot(reflectionDirection, eyeDirection), 0.0), 20.0);

            vec4 color = vec4((ambient + diffuse + specular) * baseColor.rgb, vColor.a);
            color.rgb *= color.a;
            // 没有给glColor 赋值 
            float w = weight(gl_FragCoord.z, color.a);
            accumColor = vec4(color.rgb * w, color.a);// 给颜色乘上深度权重
            accumAlpha = color.a * w;
        }
    </script>

    <!-- 最终绘图的 只绘制一个矩形面 -->
    <script type="x-shader/x-vertex" id="vertex-quad">
        #version 300 es

        layout(location=0) in vec4 aPosition;
        
        void main() {
            gl_Position = aPosition;
        }
    </script>
    <script type="x-shader/x-fragment" id="fragment-draw">
        #version 300 es
        precision highp float;

        uniform sampler2D uAccumulate;
        uniform sampler2D uAccumulateAlpha;
        out vec4 fragColor; //  这个out 去了哪里
        void main() {
            ivec2 fragCoord = ivec2(gl_FragCoord.xy);
            vec4 accum = texelFetch(uAccumulate, fragCoord, 0);
            float a = 1.0 - accum.a;
            accum.a = texelFetch(uAccumulateAlpha, fragCoord, 0).r;
            fragColor = vec4(a * accum.rgb / clamp(accum.a, 0.001, 50000.0), a); // 在只有一个四维输出的情况下这就等于是给gl_Color赋值
        }
    </script>
    <script src="./three.min.js"></script>
    <script src="./OrbitControls.js"></script>
    <script>
        var canvas = document.getElementById("gl-canvas");
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;

 
        let    camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, .1, 10 );
				camera.position.set( 0, 0.8, 2 );

				// controls
				var controls = new THREE.OrbitControls( camera, canvas );
				controls.minDistance = .1;
				controls.maxDistance = 150;
				controls.screenSpacePanning = true
                


        var gl = canvas.getContext("webgl2");

        if (!gl) {
            console.error("WebGL 2 not available");
            document.body.innerHTML = "This example requires WebGL 2 which is unavailable on this system."
        }

        gl.clearColor(0.0, 0.0, 0.0, 1.0);
        gl.enable(gl.BLEND);
        gl.depthMask(false);// 场景全是透明物体，关了就没开过，这里不影响帧缓冲区的深度写入

        if (!gl.getExtension("EXT_color_buffer_float")) {
            console.error("FLOAT color buffer not available");
            document.body.innerHTML = "This example requires EXT_color_buffer_float which is unavailable on this system."
        }

        /////////////////////////
        // OBJECT DESCRIPTIONS
        /////////////////////////

        var NUM_SPHERES = 32;
        var NUM_PER_ROW = 8;
        var RADIUS = 0.6;
        var spheres = new Array(NUM_SPHERES);

        var colorData = new Float32Array(NUM_SPHERES * 4);
        var modelMatrixData = new Float32Array(NUM_SPHERES * 16);

        for (var i = 0; i < NUM_SPHERES; ++i) {
            var angle = 2 * Math.PI * (i % NUM_PER_ROW) / NUM_PER_ROW;
            var x = Math.sin(angle) * RADIUS;
            var y = Math.floor(i / NUM_PER_ROW) / (NUM_PER_ROW / 4) - 0.75;
            var z = Math.cos(angle) * RADIUS;
            spheres[i] = {
                scale: [0.8, 0.8, 0.8],
                rotate: [0, 0, 0], // Will be used for global rotation
                translate: [x, y, z],
                modelMatrix: mat4.create()
            };

            colorData.set(vec4.fromValues(
                Math.sqrt(Math.random()),
                Math.sqrt(Math.random()),
                Math.sqrt(Math.random()),
                0.5
            ), i * 4);
        }

        /////////////////////////
        // ACCUMULATION PROGRAM
        /////////////////////////

        var accumVsSource =  document.getElementById("vertex-accum").text.trim();
        var accumFsSource =  document.getElementById("fragment-accum").text.trim();
        
        var accumVertexShader = gl.createShader(gl.VERTEX_SHADER);
        gl.shaderSource(accumVertexShader, accumVsSource);
        gl.compileShader(accumVertexShader);

        if (!gl.getShaderParameter(accumVertexShader, gl.COMPILE_STATUS)) {
            console.error(gl.getShaderInfoLog(accumVertexShader));
        }

        var accumFragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
        gl.shaderSource(accumFragmentShader, accumFsSource);
        gl.compileShader(accumFragmentShader);

        if (!gl.getShaderParameter(accumFragmentShader, gl.COMPILE_STATUS)) {
            console.error(gl.getShaderInfoLog(accumFragmentShader));
        }

        var accumProgram = gl.createProgram();
        gl.attachShader(accumProgram, accumVertexShader);
        gl.attachShader(accumProgram, accumFragmentShader);
        gl.linkProgram(accumProgram);

         console.log(gl.getProgramParameter(accumProgram, gl.ACTIVE_UNIFORMS))   
        if (!gl.getProgramParameter(accumProgram, gl.LINK_STATUS)) {
            console.error(gl.getProgramInfoLog(accumProgram));
        }

        /////////////////////
        // DRAW PROGRAM
        /////////////////////

        var quadVsSource =  document.getElementById("vertex-quad").text.trim();
        var drawFsSource = document.getElementById("fragment-draw").text.trim();

        var drawVertexShader = gl.createShader(gl.VERTEX_SHADER);
        gl.shaderSource(drawVertexShader, quadVsSource);
        gl.compileShader(drawVertexShader);

        if (!gl.getShaderParameter(drawVertexShader, gl.COMPILE_STATUS)) {
            console.error(gl.getShaderInfoLog(drawVertexShader));
        }

        var drawFragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
        gl.shaderSource(drawFragmentShader, drawFsSource);
        gl.compileShader(drawFragmentShader);

        if (!gl.getShaderParameter(drawFragmentShader, gl.COMPILE_STATUS)) {
            console.error(gl.getShaderInfoLog(drawFragmentShader));
        }

        var drawProgram = gl.createProgram();
        gl.attachShader(drawProgram, drawVertexShader);
        gl.attachShader(drawProgram, drawFragmentShader);
        gl.linkProgram(drawProgram);

        if (!gl.getProgramParameter(drawProgram, gl.LINK_STATUS)) {
            console.error(gl.getProgramInfoLog(drawProgram));
        }

        /////////////////////////
        // GET UNIFORM LOCATIONS
        /////////////////////////
        //  获取自定义的 结构体 uniform 
        var sceneUniformsLocation = gl.getUniformBlockIndex(accumProgram, "SceneUniforms");
        gl.uniformBlockBinding(accumProgram, sceneUniformsLocation, 0);

        var modelMatrixLocation = gl.getUniformLocation(accumProgram, "uModel");
        var textureLocation = gl.getUniformLocation(accumProgram, "uTexture");

        var accumLocation = gl.getUniformLocation(drawProgram, "uAccumulate");
        var accumAlphaLocation = gl.getUniformLocation(drawProgram, "uAccumulateAlpha");

        ////////////////////////////////
        //  SET UP FRAMEBUFFERS
        ////////////////////////////////

        var accumBuffer = gl.createFramebuffer();// 帧缓冲区唯一 

        gl.bindFramebuffer(gl.FRAMEBUFFER, accumBuffer);
        gl.activeTexture(gl.TEXTURE0);

        var accumTarget = gl.createTexture(); //暂时的 0  后面和 layout out accumColor 绑定
        gl.bindTexture(gl.TEXTURE_2D, accumTarget);
        gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
        gl.texStorage2D(gl.TEXTURE_2D, 1, gl.RGBA16F, gl.drawingBufferWidth, gl.drawingBufferHeight);
        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, accumTarget, 0);

        var accumAlphaTarget = gl.createTexture(); // 暂时的 0 这个缓冲区绑定的输出只有一位 alpha   后面和 layout out accumAlpha 绑定
        gl.bindTexture(gl.TEXTURE_2D, accumAlphaTarget);
        gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
        // webgl2 指定纹理存储级别    target    levels internalformat   widit         height
        gl.texStorage2D(gl.TEXTURE_2D, 1, gl.R16F, gl.drawingBufferWidth, gl.drawingBufferHeight);
        // webgl2 可用 COLOR_ATTACHMENT1
        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT1, gl.TEXTURE_2D, accumAlphaTarget, 0);

        var depthTarget = gl.createTexture();  // 2 和深度缓冲区绑定
        gl.bindTexture(gl.TEXTURE_2D, depthTarget);
        gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
        gl.texStorage2D(gl.TEXTURE_2D, 1, gl.DEPTH_COMPONENT16, gl.drawingBufferWidth, gl.drawingBufferHeight);
        //帧缓冲区的深度缓冲区
        //                        target         attachment           textarget      texture    level
        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, depthTarget, 0);

        // 将颜色写入第一和第二个 帧缓冲区,这个正对应着色器中的 out 输出 这个方法应是关联输出和attachment
        // The number of output locations available to a shader is limited. For fragment shaders, the limit is the advertised number of draw buffer 
        // 输出变量的数量限制，对于片元着色器来说就是 已经声明了的 绘制 缓冲区（drawbuffer）

         gl.drawBuffers([
            gl.COLOR_ATTACHMENT0,
            gl.COLOR_ATTACHMENT1
        ]);
        // 绑定了帧缓冲区，初始化了三个纹理作为渲染目标

        gl.bindFramebuffer(gl.FRAMEBUFFER, null);

        /////////////////////
        // SET UP GEOMETRY
        /////////////////////

        var sphere = utils.createSphere({radius: 0.5});
        var numVertices = sphere.positions.length / 3;

        var sphereArray = gl.createVertexArray();
        gl.bindVertexArray(sphereArray);

        var positionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, sphere.positions, gl.STATIC_DRAW);
        gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
        gl.enableVertexAttribArray(0);

        var uvBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, uvBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, sphere.uvs, gl.STATIC_DRAW);
        gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);
        gl.enableVertexAttribArray(1);

        var normalBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, sphere.normals, gl.STATIC_DRAW);
        gl.vertexAttribPointer(2, 3, gl.FLOAT, false, 0, 0);
        gl.enableVertexAttribArray(2);


        var color = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, color);
        gl.bufferData(gl.ARRAY_BUFFER, colorData, gl.STATIC_DRAW);
        gl.vertexAttribPointer(3, 4, gl.FLOAT, false, 0, 0);
        gl.vertexAttribDivisor(3, 1);
        gl.enableVertexAttribArray(3);

        // Columns of matrix as separate attributes for instancing
        var matrixBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, matrixBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, modelMatrixData, gl.DYNAMIC_DRAW);
        gl.vertexAttribPointer(4, 4, gl.FLOAT, false, 64, 0);
        gl.vertexAttribPointer(5, 4, gl.FLOAT, false, 64, 16);
        gl.vertexAttribPointer(6, 4, gl.FLOAT, false, 64, 32);
        gl.vertexAttribPointer(7, 4, gl.FLOAT, false, 64, 48);

        gl.vertexAttribDivisor(4, 1);
        gl.vertexAttribDivisor(5, 1);
        gl.vertexAttribDivisor(6, 1);
        gl.vertexAttribDivisor(7, 1);

        gl.enableVertexAttribArray(4);
        gl.enableVertexAttribArray(5);
        gl.enableVertexAttribArray(6);
        gl.enableVertexAttribArray(7);

        var indices = gl.createBuffer();
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indices);
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, sphere.indices, gl.STATIC_DRAW);
        // 已经传入顶点 法线 颜色数据
        // Quad for draw pass
        var quadArray = gl.createVertexArray();
        gl.bindVertexArray(quadArray);

        var quadPositionBuffer = gl.createBuffer();// 创建了一个缓冲区用于存放顶点数据
        gl.bindBuffer(gl.ARRAY_BUFFER, quadPositionBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([  // 这个面就是近裁剪面
            -1, 1,
            -1, -1,
            1, -1,
            -1, 1,
            1, -1,
            1, 1,
        ]), gl.STATIC_DRAW);
        gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); // 设置顶点数据的读取方式
        gl.enableVertexAttribArray(0);

        //////////////////////
        // SET UP UNIFORMS
        //////////////////////

        var projMatrix = mat4.create();
        mat4.perspective(projMatrix, Math.PI / 2, canvas.width / canvas.height, 0.1, 10.0);

        var viewMatrix = mat4.create();
        var eyePosition = vec3.fromValues(0, 0.8, 2);
        mat4.lookAt(viewMatrix, eyePosition, vec3.fromValues(0, 0, 0), vec3.fromValues(0, 1, 0));

        var viewProjMatrix = mat4.create();
        mat4.multiply(viewProjMatrix, projMatrix, viewMatrix);

        var lightPosition = vec3.fromValues(1, 1, 2);
        
        var sceneUniformData = new Float32Array(24);
        sceneUniformData.set(viewProjMatrix);
        sceneUniformData.set(eyePosition, 16);
        sceneUniformData.set(lightPosition, 20);

        var sceneUniformBuffer = gl.createBuffer();
        gl.bindBufferBase(gl.UNIFORM_BUFFER, 0, sceneUniformBuffer);
        gl.bufferData(gl.UNIFORM_BUFFER, sceneUniformData, gl.STATIC_DRAW);

        var image = new Image();

        image.onload = function() {

            ///////////////////////
            // BIND TEXTURES
            ///////////////////////

            var texture = gl.createTexture(); //0
            gl.activeTexture(gl.TEXTURE0);
            gl.bindTexture(gl.TEXTURE_2D, texture);

            gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
            gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
            
            var levels = levels = Math.floor(Math.log2(Math.max(this.width, this.height))) + 1;
            gl.texStorage2D(gl.TEXTURE_2D, levels, gl.RGBA8, image.width, image.height);
            gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, image.width, image.height, gl.RGBA, gl.UNSIGNED_BYTE, image);
            gl.generateMipmap(gl.TEXTURE_2D);

            gl.activeTexture(gl.TEXTURE1);
            gl.bindTexture(gl.TEXTURE_2D, accumTarget); // 1

            gl.activeTexture(gl.TEXTURE2);
            gl.bindTexture(gl.TEXTURE_2D, accumAlphaTarget); // 2

            gl.useProgram(accumProgram);
            gl.uniform1i(textureLocation, 0);// 场景要用的纹理贴图

            gl.useProgram(drawProgram);
            gl.uniform1i(accumLocation, 1);// 这个纹理编号是1  accumTarget
            gl.uniform1i(accumAlphaLocation, 2);// 2号纹理就是 accumAlphaTarget

            var rotationMatrix = mat4.create();

            controls.addEventListener('change', ()=> { 
                sceneUniformData.set(camera.matrixWorldInverse.clone().multiply(camera.projectionMatrix).elements);
                sceneUniformData.set(camera.position,16);
                      
            gl.bindBufferBase(gl.UNIFORM_BUFFER, 0, sceneUniformBuffer);
             gl.bufferData(gl.UNIFORM_BUFFER, sceneUniformData, gl.STATIC_DRAW);
            })
            function draw() {

                ////////////////////
                // DRAW BOXES
                ////////////////////

                gl.bindFramebuffer(gl.FRAMEBUFFER, accumBuffer);// 前面定义的帧缓冲区 ，绘图会更新至对应纹理
                gl.useProgram(accumProgram);
                gl.bindVertexArray(sphereArray);
                // 更新矩阵罢了
                for (var i = 0, len = spheres.length; i < len; ++i) {
                    spheres[i].rotate[1] += 0.002;

                    utils.xformMatrix(spheres[i].modelMatrix, spheres[i].translate, null, spheres[i].scale);
                    mat4.fromYRotation(rotationMatrix, spheres[i].rotate[1]);
                    mat4.multiply(spheres[i].modelMatrix, rotationMatrix, spheres[i].modelMatrix);
                
                    modelMatrixData.set(spheres[i].modelMatrix, i * 16);
                }
                // 

                gl.bindBuffer(gl.ARRAY_BUFFER, matrixBuffer);         // 给矩阵单独稍微 缓冲区
                gl.bufferSubData(gl.ARRAY_BUFFER, 0, modelMatrixData); // 更新缓冲区的一个子集



                // 设置要使用的混合函数   rgbsrc  rgbdst    asrc    adst  (sColor.rgb + dColor.rgb,DstColor.a* (1- sColor.a)  )
                gl.blendFuncSeparate(gl.ONE, gl.ONE, gl.ZERO, gl.ONE_MINUS_SRC_ALPHA);
                gl.clear(gl.COLOR_BUFFER_BIT);
                // mode count type(bit short int)  offset  instanceCount   能够执行一组元素的多个实例
                gl.drawElementsInstanced(gl.TRIANGLES, sphere.indices.length, gl.UNSIGNED_SHORT, 0, spheres.length);


                /////////
                // DRAW
                /////////

                gl.bindFramebuffer(gl.FRAMEBUFFER, null);
                gl.useProgram(drawProgram);// 切换着色器
                gl.bindVertexArray(quadArray);// 切换 顶点数据缓冲 绘制一个面
                gl.clear(gl.COLOR_BUFFER_BIT);
                // 和前面的区别大概就是 这个是简写 哈哈 （rgb, 1- srcA）
                 gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA); // 既然绘制一个面了这个融合算法有必要吗，试了一下还真的有必要
                gl.drawArrays(gl.TRIANGLES, 0, 6);                

                requestAnimationFrame(draw);
            }

            requestAnimationFrame(draw);
            
        }

        image.src = "img/khronos_webgl.png";

    </script>
    <a href="https://github.com/tsherif/webgl2examples" id="github-ribbon"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
</body>
</html>
